package cate.game.activity.wishtower;

import cate.common.table.activity.wishtower.row.WishTowerBigPrizeRow;
import cate.common.table.activity.wishtower.row.WishTowerCostRow;
import cate.common.table.activity.wishtower.row.WishTowerItemRow;
import cate.common.util.GameResult;
import cate.common.util.random.RWList;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.wishtower.msg.WishTowerDataResp;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class RoleWishTower extends RoleActivitySingle {
    @NoteField("大奖领取记录")
    public List<Integer> bigPrizeGot;

    @NoteField("本轮奖励获取记录 里面记录的位置1~10")
    public List<Byte> rewardGot;

    @NoteField("当前选择的大奖tid")
    public int chooseBigPrizeId;

    @NoteField("当前轮")
    public int turn;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (bigPrizeGot == null) {
            bigPrizeGot = new ArrayList<>();
        }
        if (rewardGot == null) {
            rewardGot = new ArrayList<>();
        }
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    private void noticeUpdate() {
        role.sendNow(new WishTowerDataResp(this));
    }

    public GameResult<Void> chooseBigPrize(int tid) {
        GameResult<Void> r = new GameResult<>();
        if (chooseBigPrizeId != 0) {
            return r.fail("您已经选择过大奖了");
        }
        if (bigPrizeGot.contains(tid)) {
            return r.fail("您无法选择已经抽中过的大奖");
        }
        WishTowerBigPrizeRow row = role.getGame().table.wishTower.bigPrize.get(tid);
        if (row == null) {
            return r.fail("不能选择该终极大奖噢");
        }
        if (row.configId != configTid()) {
            return r.fail("不能选择该终极大奖噢");
        }
        if (bigPrizeAllGot()) {
            return r.fail("您已经领完所有终极大奖拉");
        }
        chooseBigPrizeId = tid;
        noticeUpdate();
        return r;
    }

    public GameResult<Void> drawItem() {
        GameResult<Void> r = new GameResult<>();
        if (chooseBigPrizeId == 0) {
            return r.fail("请先选择终极大奖");
        }
        WishTowerBigPrizeRow bigPrizeRow = role.getGame().table.wishTower.bigPrize.get(chooseBigPrizeId);
        if (bigPrizeRow == null) {
            return r.fail("终极大奖不见拉");
        }
        int times = rewardGot.size();
        WishTowerCostRow row = role.getGame().table.wishTower.cost.get(times + 1);
        if (row == null) {
            return r.fail("哎呀没有消耗，您不能抽奖噢");
        }
        if (StringUtils.isNotBlank(row.costStr)) {
            GameResult<MixResItem> consumeRes = new MixRes(row.costStr).consume(role);
            if (!consumeRes.ok()) {
                role.getGame().notice.tipResItem(role, consumeRes.data);
                return r.fail(consumeRes);
            }
        }
        byte pos = draw(bigPrizeRow);
        if (pos == -1) {
            return r.fail("哎呀，怎么抽不到东西呢");
        }
        if (pos == bigPrizeRow.pos) {
            bigPrizeGot.add(bigPrizeRow.id);
        }
        rewardGot.add(pos);
        if (prizeAllGot()) {
            chooseBigPrizeId = 0;
            rewardGot.clear();
            turn ++;
        }
        noticeUpdate();
        return r;
    }

    private byte draw(WishTowerBigPrizeRow bigPrizeRow) {
        List<WishTowerItemRow> items = role.getGame().table.wishTower.item.getDrawList(configTid(), turn, rewardGot);
        RWList<WeightPosItem> posItems = new RWList<>();
        if (CollectionUtils.isNotEmpty(items)) {
            for (WishTowerItemRow row : items) {
                WeightPosItem item = new WeightPosItem(row.pos, row.weight);
                posItems.add(item);
            }
        }
        if (!rewardGot.contains(bigPrizeRow.pos)) {
            posItems.add(new WeightPosItem(bigPrizeRow.pos, bigPrizeRow.weight));
        }
        if (posItems.isEmpty()) {
            return -1;
        }
        return posItems.setup().get().pos;
    }

    private boolean prizeAllGot() {
        return role.getGame().table.wishTower.item.getItemNums(configTid(), turn) + 1 <= rewardGot.size();
    }

    private boolean bigPrizeAllGot() {
        return role.getGame().table.wishTower.bigPrize.getBigPrizeNums(configTid()) <= bigPrizeGot.size();
    }

    @Override
    public void onRoleOpen() {
        turn = 1;
    }

    @Override
    public void onRoleEnd() {
        turn = 0;
        chooseBigPrizeId = 0;
        bigPrizeGot.clear();
        rewardGot.clear();
    }
}
